<?php

namespace App\Http\Controllers\Schedule\Merchant;

use App\Http\Controllers\BaseController;
use App\Service\BaseService;
use App\Service\Schedule\ScheduleService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ScheduleController extends BaseController
{


    /**
     * 班次管理-列表
     * @param Request $request
     * @return JsonResponse
     */
    public function rulesList(Request $request): JsonResponse
    {
        $shopId    = (int)$request->header('shopId', 0);
        if (!$shopId) {
            return BaseService::throwErr("shopId错误");
        }
        return $this->success(ScheduleService::rulesList($shopId));
    }

    /**
     * 班次管理-修改
     * @param Request $request
     * @return JsonResponse
     */
    public function rulesUpdate(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'name'  => 'required|string',
                'start' => 'required|string',
                'end'   => 'required|string',
            ]
        );
        if ($request->start == $request->end) {
            return BaseService::throwErr("结束时间应不等于开始时间");
        }
        $id       = (int)$request->get('id', 0);
        $color    = (string)$request->get('color', '');
        $duration = (int)$request->get('duration', 0);
        $shopId   = (int)$request->header('shopId', 0);
        if (!$shopId) {
            return BaseService::throwErr("shopId错误");
        }
        $userId = getRmsUserId();
        // 计算小时数
        if ($request->end > $request->start) {
            $duration = intval($request->end) - intval($request->start);
        } else {
            $duration = (24 - intval($request->start)) + intval($request->end);
        }
        if ($duration < 0) {
            return BaseService::throwErr("班次时间错误");
        }
        ScheduleService::rulesUpdate($shopId, $userId, $id, $request->name, $request->start, $request->end, $color, $duration);
        return $this->success();
    }

    /**
     * 班次管理-删除
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function rulesDelete(Request $request, int $id): JsonResponse
    {
        $shopId = (int)$request->header('shopId', 0);
        if (!$shopId) {
            return BaseService::throwErr("shopId错误");
        }
        $userId = getRmsUserId();
        ScheduleService::rulesDelete($shopId, $id, $userId);
        return $this->success();
    }


    /**
     * 班次管理-提醒开关
     * @param Request $request
     * @return JsonResponse
     */
    public function remindSwitch(Request $request): JsonResponse
    {
        $userId = getRmsUserId();
        return $this->success(ScheduleService::remindSwitch($userId));
    }

    /**
     * 班次管理-提醒开关修改
     * @param Request $request
     * @return JsonResponse
     */
    public function remindSwitchUpdate(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'data' => 'required|array',
            ]
        );
        $userId = getRmsUserId();
        ScheduleService::remindSwitchUpdate($userId, $request->data);
        return $this->success();
    }

    /**
     * 班次管理-班次统计
     * @param Request $request
     * @return JsonResponse
     */
    public function usersTotal(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'startTime' => 'required|date',
                'endTime'   => 'required|date',
            ]
        );
        $userId = (string)$request->get('userId', '');
        $shopId = (int)$request->header('shopId', 0);
        if (!$shopId) {
            return BaseService::throwErr("shopId错误");
        }
        $userId = $userId ? explode(",", $userId) : [];
        return $this->success(ScheduleService::usersTotal($shopId, $userId, $request->startTime, $request->endTime));
    }

    /**
     * 店长排班预览
     * @param Request $request
     */
    public function scheduleOverviewManager(Request $request)
    {
        $this->validate($request, [
            'startTime' => 'required|date',
            'endTime'   => 'required|date',
        ]);
        $userIds = (string)$request->get('userIds', '');
        $shopId  = (int)$request->header('shopId', 0);
        if ($userIds) {
            $userIds = explode(",", $userIds);
        } else {
            $userIds = [];
        }

        return $this->success(ScheduleService::scheduleOverviewManager($shopId, $userIds, $request->startTime, $request->endTime));
    }

    /**
     * 排班入口页面
     * @param Request $request
     * @return JsonResponse
     */
    public function scheduleEntrance(Request $request)
    {
        $this->validate($request, [
            'startTime' => 'required|date',
            'endTime'   => 'required|date',
        ]);
        $shopId = (int)$request->header('shopId', 0);

        $data = [
            'table'         => ScheduleService::scheduleEntrance($shopId, $request->startTime, $request->endTime),
            'scheduleRules' => ScheduleService::rulesList($shopId),
        ];

        return $this->success($data);
    }

    /**
     * 员工排班预览
     * @param Request $request
     * @return JsonResponse
     */
    public function preview(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'startTime' => 'required|date',
                'endTime'   => 'required|date',
                'userId'   => 'required|integer',
            ]
        );
        $shopId = (int)$request->header('shopId', 0);
        if (!$shopId) {
            return BaseService::throwErr("shopId错误");
        }
        return $this->success(ScheduleService::preview($shopId, $request->userId, $request->startTime, $request->endTime));
    }

    /**
     * 员工列表
     * @param Request $request
     * @return JsonResponse
     */
    public function scheduleStaff(Request $request): JsonResponse
    {
        $shopId    = (int)$request->header('shopId', 0);
        if (!$shopId) {
            return BaseService::throwErr("shopId错误");
        }
        return $this->success(ScheduleService::scheduleStaff($shopId));
    }

}
